#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define MAX_N 200000
#define int long long
#define mk make_pair
#define pi pair
const int mod = 998244353;
int n, S;
vector<pi<int, pi<int, int>>> e;
int fa[MAX_N + 5];
int siz[MAX_N + 5];
void init() {
	e.clear();
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
		siz[i] = 1;
	}
	return;
}
int find(int x) {
	return fa[x] = (fa[x] == x ? x : find(fa[x]));
}
void merge(int a, int b) {
	if (find(a) == find(b)) return;
	siz[find(a)] %= mod;
	siz[find(b)] %= mod;
	siz[find(b)] += siz[find(a)];
	siz[find(b)] %= mod;
	fa[find(a)] = find(b);
	return;
}

int qpow(int a, int k) {
	int ans = 1;
	while (k) {
		if (k & 1) ans = (ans * a) % mod;
		k >>= 1;
		a = (a * a) % mod;
	}
	return ans;
}

bool cmp(const pi<int, pi<int, int>>& a, const pi<int, pi<int, int>>& b) {
	return a.first < b.first;
}

void solve() {
	cin >> n >> S;
	init();
	for (int i = 1; i < n; i++) {
		int u, v, w; cin >> u >> v >> w;
		e.push_back(mk(w, mk(u, v)));
	}
	sort(e.begin(), e.end(), cmp);
	int ans = 1;
	for (auto temp : e) {
		int w = temp.first;
		int u = temp.second.first;
		int v = temp.second.second;
		int cnt = (S - w) + 1;
		int p = (siz[find(u)] * siz[find(v)]) - 1;
		ans = ans * qpow(cnt, p) % mod;
		merge(u, v);
	}
	cout << ans << endl;
	return;
}

int32_t main() {
	int t; cin >> t;
	while (t--) solve();
	return 0;
}